<%--
  Created by IntelliJ IDEA.
  User: B饭老司机
  Date: 2024/3/23
  Time: 16:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<html>

<head>
    <meta charset="utf-8">
    <title>Bootstrap 布局实例</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="js/user.js"></script>
    <link rel="stylesheet" href="css/user.css">
    <style>
        td, th {
            text-align: center;
        }

        .custom-smaller-input {
            font-size: 1.25rem;
        }
    </style>
</head>

<body>

<div class="container-fluid">
    <div class="row">

        <div class="col-sm-12" id="right">
            <ol class="breadcrumb">
                <li class="active">菜单
                </li>
                <li class="active">用户信息
                </li>
            </ol>
            <%
                String name = "";
                String worknumber = "";
                String department = "";
                String worktime = "";
                String profession = "";
                if (request.getParameter("name") != null) name = request.getParameter("name");
                if (request.getParameter("worknumber") != null) worknumber = request.getParameter("worknumber");
                if (request.getParameter("department") != null) department = request.getParameter("department");
                if (request.getParameter("worktime") != null) worktime = request.getParameter("worktime");
                if (request.getParameter("profession") != null) profession = request.getParameter("profession");
            %>
            <div class="panel panel-default">
                <div class="panel-heading">
                    搜索
                </div>
                <div class="panel-body">
                    <form role="form" class="form-inline" action="person_list.jsp" method="post">
                        <div class="form-group">
                            <label for="name">工号</label>
                            <input type="text" class="form-control custom-smaller-input" id="worknumber"
                                   name="worknumber" value="<%=worknumber%>" placeholder="请输入工号">
                        </div>
                        <div class="form-group">
                            <label for="name">姓名</label>
                            <input type="text" class="form-control custom-smaller-input" id="name" name="name"
                                   value="<%=name%>" placeholder="请输入姓名">
                        </div>
                        <div class="form-group">
                            <label for="name">入职时间</label>
                            <input type="date" class="form-control custom-smaller-input" id="worktime" name="worktime"
                                   value="<%=worktime%>">
                        </div>
                        <div class="form-group">
                            <label for="name">部门</label>
                            <input type="text" class="form-control custom-smaller-input" id="department" name="department"
                                   value="<%=department%>" placeholder="请输入部门">
                        </div>
                        <div class="form-group">
                            <label for="name">专业</label>
                            <input type="text" class="form-control custom-smaller-input" id="profession"
                                   name="profession" value="<%=profession%>" placeholder="请输入专业">
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">开始搜索</button>
                        </div>
                        <div class="form-group">
                            <a class="btn btn-default" href="person_add.jsp">添加</a>
                        </div>
                    </form>
                </div>
            </div>
            <!--
            列表展示
        -->
            <div class="table-responsive">
                <table class="table table-striped ">
                    <thead>
                    <tr>
                        <%--<th>序号</th>--%>
                        <th>工作编号</th>
                        <th>姓名</th>
                        <th>电话号码</th>
                        <th>部门</th>
                        <th>入职时间</th>
                        <th>学历</th>
                        <th>专业</th>
                        <th>职称</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <%
                        Connection conn = (Connection) session.getAttribute("Connection");
                        String sql = "select * from people ";
                        if (name != "" || worknumber != "" || department != "" || worktime != "" || profession != "") {
                            sql = sql + "where ";
                            if (name != "") {
                                sql += "name like '%" + name + "%' ";
                                if (worknumber != "") {
                                    sql += "and worknumber='" + worknumber + "' ";
                                }
                                if (department != "") {
                                    sql += "and department='" + department + "' ";
                                }
                                if (worktime != "") {
                                    sql += "and worktime='" + worktime + "' ";
                                }
                                if (profession != "") {
                                    sql += "and profession='" + profession + "' ";
                                }
                            } else {
                                if (worknumber != "") {
                                    sql += "worknumber='" + worknumber + "' ";
                                    if (department != "") {
                                        sql += "and department='" + department + "' ";
                                    }
                                    if (worktime != "") {
                                        sql += "and worktime='" + worktime + "' ";
                                    }
                                    if (profession != "") {
                                        sql += "and profession='" + profession + "' ";
                                    }
                                } else {
                                    if (department != "") {
                                        sql += "department='" + department + "' ";
                                        if (worktime != "") {
                                            sql += "and worktime='" + worktime + "' ";
                                        }
                                        if (profession != "") {
                                            sql += "and profession='" + profession + "' ";
                                        }
                                    } else {
                                        if (worktime != "") {
                                            sql += "worktime='" + worktime + "' ";
                                            if (profession != "") {
                                                sql += "and profession='" + profession + "' ";
                                            }
                                        } else {
                                            if (profession != "") {
                                                sql += "profession='" + profession + "' ";
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        int curpage = 1;
                        if (request.getParameter("page") != null) {
                            String pg = request.getParameter("page");
                            curpage = Integer.parseInt(pg);
                        }
                        int al = 0; //al为-1时表示上一页，为1时表示下一页，默认为0
                        if (request.getParameter("alterpage") != null) {
                            String alpg = request.getParameter("alterpage");
                            al = Integer.parseInt(alpg);
                        }
                        if (curpage > 1 || al > 0) {
                            curpage += al;
                        }
                        int page_size = 6;
                        int start = curpage * page_size - page_size;
                        String pge = "LIMIT " + start + ", " + page_size;
                        sql = sql + pge;

                        PreparedStatement pstmt = conn.prepareStatement(sql);
                        ResultSet rs = pstmt.executeQuery();

                        while (rs.next()) {
                    %>
                    <tr>
                        <td><%=rs.getString("worknumber")%>
                        </td>
                        <td><%=rs.getString("name")%>
                        </td>
                        <td><%=rs.getString("phonenumber")%>
                        </td>
                        <td><%=rs.getString("department")%>
                        </td>
                        <td><%=rs.getString("worktime")%>
                        </td>
                        <td><%=rs.getString("degree")%>
                        </td>
                        <td><%=rs.getString("profession")%>
                        </td>
                        <td><%=rs.getString("title")%>
                        </td>
                        <td>
                            <div class="btn-group">
                                <a class="btn btn-default" href="person_edit.jsp?id=<%=rs.getInt("id")%>" >修改</a>
                                <a class="btn btn-danger" href="#" onclick="confirmDelete(<%=rs.getInt("id")%>)">删除</a>
                            </div>
                        </td>
                    </tr>
                    <%
                        }
                    %>

                    </tbody>
                </table>
            </div>
            <ul class="pagination" style="float: right;">
                <li>
                    <a href="person_list.jsp?alterpage=-1&page=<%=curpage%>&name=<%=name%>&worknumber=<%=worknumber%>&department=<%=department%>&worktime=<%=worktime%>&profession=<%=profession%>">&laquo;</a>
                </li>
                <%
                    int psize = 5; //页面号码标签数量
                    int ppg = psize * ((curpage - 1) / psize + 1);
                    for (int i = ppg - psize + 1; i <= ppg; i++) {
                        if (i == curpage) {
                            out.println("<li class=\"active\"><a href=\"person_list.jsp?page=" + i + "&name=" + name + "&worknumber=" + worknumber + "&department=" + department + "&worktime=" + worktime + "&profession=" + profession + "\">" + i + "</a></li>");

                        } else {
                            out.println("<li><a href=\"person_list.jsp?page=" + i + "&name=" + name + "&worknumber=" + worknumber + "&department=" + department + "&worktime=" + worktime + "&profession=" + profession + "\">" + i + "</a></li>");
                        }
                    }
                %>
                <li>
                    <a href="person_list.jsp?alterpage=1&page=<%=curpage%>&name=<%=name%>&worknumber=<%=worknumber%>&department=<%=department%>&worktime=<%=worktime%>&profession=<%=profession%>">&raquo;</a>
                </li>
            </ul>
        </div>
    </div>
</div>
<!-- 底部页脚部分 -->
<%--<div class="footer">--%>
<%--    <p class="text-center">--%>
<%--        2024 &copy;--%>
<%--    </p>--%>
<%--</div>--%>
</body>
<script>
    function confirmDelete(id) {
        if (confirm("确定要删除吗？")) {
            window.location.href = "person_delete.jsp?id=" + id + "&page=<%=curpage%>&name=<%=name%>&worknumber=<%=worknumber%>&department=<%=department%>&worktime=<%=worktime%>&profession=<%=profession%>";
        }
    }
</script>
</html>